package id.net.iconpln.bb.client.view;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Element;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore; 
import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbStockBean;
import id.net.iconpln.bb.client.model.PageBbSurveyorBean;
import id.net.iconpln.bb.client.model.PageBbTransportirBean;
import id.net.iconpln.bb.client.model.Stock;
import java.util.ArrayList;  
import java.util.List;  

public class PageBbTransportir extends LayoutContainer {

	Grid<PageBbTransportirBean> grid;
	private FormData formData;
	private FormBinding formBindings;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Master Transportir");  
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbTransportirBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbTransportirBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true);
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(grid, new RowData(.6, 1)); 
	    add(cp, new FitData(5)); 
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Form Master Data Transportir");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2);
	    
	    final TextField<String> txtnama = new TextField<String>();
	    txtnama.setName("nama_transportir");
	    txtnama.setFieldLabel("Nama Transportir");  
	    fieldSet2.add(txtnama, formData);
	    
	    final SimpleComboBox<String> txtjenis = new SimpleComboBox<String>();  
	    txtjenis.setFieldLabel("Jenis Transportir");  
	    txtjenis.setForceSelection(true);  
	    txtjenis.setTypeAhead(true);   
	    txtjenis.setTriggerAction(TriggerAction.ALL);
	    txtjenis.add("Kapal");
	    txtjenis.add("Tongkang");
	    txtjenis.add("Truck");
	    txtjenis.add("Conveyor");
	    fieldSet2.add(txtjenis, formData);  
	  
	    final TextField<String> txtpemilik = new TextField<String>();
	    txtpemilik.setName("pemilik");
	    txtpemilik.setFieldLabel("Pemilik");  
	    fieldSet2.add(txtpemilik, formData);
	    
	    final TextField<String> txtpenyewa = new TextField<String>();
	    txtpenyewa.setName("penyewa");
	    txtpenyewa.setFieldLabel("Penyewa");  
	    fieldSet2.add(txtpenyewa, formData);
	    
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        txtjenis.setValue(null);
	        txtnama.setValue("");
	        txtpemilik.setValue("");
	        txtpenyewa.setValue("");
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();
	        MessageBox.alert("Message", "DATABASE sedang dalam Perbaikan", null);
	      }  
	    }));  

	    panel.add(p);
	   return panel;  
	  }  
	  
	  private Grid<PageBbTransportirBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig jenis = new ColumnConfig("jenis_transportir", "Jenis", 100);
	    jenis.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig nama = new ColumnConfig("nama_transportir", "Nama", 200);
	    nama.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig pemilik = new ColumnConfig("pemilik", "Pemilik", 200);
	    pemilik.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig penyewa = new ColumnConfig("penyewa", "Penyewa", 200);
	    penyewa.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig gps_fk = new ColumnConfig("gps_fk", "GPS FK", 100);
	    gps_fk.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig pemasok_fk = new ColumnConfig("pemasok_fk", "Pemasok FK", 100);
	    pemasok_fk.setAlignment(HorizontalAlignment.RIGHT);
	    
	    columns.add(id);
	    columns.add(nama);
	    columns.add(pemilik);
	    columns.add(penyewa);
	    columns.add(jenis);
//	    columns.add(gps_fk);
//	    columns.add(pemasok_fk);
	    
	    ListStore<PageBbTransportirBean> store = new ListStore<PageBbTransportirBean>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getTransportir());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbTransportirBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
}

